package cate.common.table.item.rune.sheet;

import cate.common.table.ESheet;
import cate.common.table.item.rune.row.RuneAttrRow;
import cate.common.util.random.RWList;
import easy.java.dev.note.NoteClass;
import org.apache.commons.collections4.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@NoteClass(value = "符文属性表")
public class RuneAttrSheet extends ESheet<RuneAttrRow> {

    @Override
    public String getSheetName() {
        return "徽章属性";
    }

    @Override
    protected RuneAttrRow buildRow() {
        return new RuneAttrRow();
    }

    private Map<Integer, RWList<RuneAttrRow>> groupMap = new HashMap<>();

    @Override
    protected void onLoaded() {
        groupMap = new HashMap<>();
        for (RuneAttrRow row : this.getList()) {
            RWList<RuneAttrRow> rowList = groupMap.get(row.groupId);
            if (rowList == null) {
                rowList = new RWList<>();
                groupMap.put(row.groupId, rowList);
            }
            rowList.add(row);
        }
        groupMap.values().forEach(RWList::setup);
    }

    public RuneAttrRow randomAttr(int groupId) {
        RWList<RuneAttrRow> rwl = groupMap.get(groupId);
        if (rwl == null) {
            return null;
        }
        return rwl.get();
    }

    public RuneAttrRow randomAttr(int groupId, List<Integer> excludes) {
        RWList<RuneAttrRow> rwl = groupMap.get(groupId);
        if (rwl == null) {
            return null;
        }
        if (CollectionUtils.isEmpty(excludes)) {
            return rwl.get();
        }
        RWList<RuneAttrRow> newRwl = new RWList<>();
        for (RuneAttrRow row : rwl.list) {
            if (!excludes.contains(row.protoId)) {
                newRwl.add(row);
            }
        }
        return newRwl.setup().get();
    }

    public RuneAttrRow getAttr(int group, int protoId) {
        for (RuneAttrRow row : this.getList()) {
            if (row.groupId == group && row.protoId == protoId) {
                return row;
            }
        }
        return null;
    }

    public boolean isNiubiAttr(int attrTid) {
        RuneAttrRow attrRow = get(attrTid);
        if (attrRow == null) {
            return false;
        }
        return attrRow.isMaxValue;
    }
}
